package test.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import test.entity.Person;

@Repository
public class PersonDao {

	@Resource private SessionFactory sessionFactory;
	
	private Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	public void addPerson(Person person) {
		this.getSession().save(person);
		
	}
	@SuppressWarnings("unchecked")
	public List<Person> getPersons() {
		return (List<Person>)this.getSession().createCriteria(Person.class).list();
	}
	public Person getPersonById(int id) {
		return (Person) this.getSession().createQuery("from Person where id = ?").setParameter(0, id).uniqueResult();
	}
	public void updatePerson(Person person) {
		this.getSession().update(person);
	}
	public void deletePersonById(int id) {
		this.getSession().createQuery("delete Person where id = ?").setParameter(0, id).executeUpdate();
	}
	public PersonDao() {
		// TODO Auto-generated constructor stub
	}

}
